/*
 * @Author: your name
 * @Date: 2021-09-22 15:53:00
 * @LastEditTime: 2021-09-23 15:51:33
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \懂吃后台管理系统e:\前端\almightyService\index.js
 */
var express = require('express');
var request = require('request');
var app = express();

//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

//查询城市的天气
app.get('/getCityWeather', function(req, res) {
    request(`http://aider.meizu.com/app/weather/listWeather?cityIds=${req.query.cityIds}`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json(response);
      }
    });
});

//获取笑话
app.get('/getJoke', function(req, res) {
    request(`http://route.showapi.com/341-2?page=${req.query.page}&maxResult=${req.query.maxResult}&showapi_appid=771566&showapi_sign=04e4b9e8ddb1444aae2e909b906924ad`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json(response);
      }
    });
});

//获取单词解释
app.get('/getWordInfo', function(req, res) {
    request(`http://dict.youdao.com/suggest?q=${req.query.q}`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json(response);
      }
    });
});

//配置服务端口
var server = app.listen(8568, function() {

    var host = server.address().address;

    var port = server.address().port;

    console.log('服务启动！', host, port);
})